package org.road0329.tools.proto_parse_tool.chargingpile;


import org.road0329.tools.proto_parse_tool.utils.ProtocolUtils;

/**
 * 数据帧
 *
 * @author road
 */
public class DataFrame {

    /**
     * 数据帧内容
     */
    private byte[] buf;
    /**
     * 数据帧长度
     */
    private short length;

    /**
     * 帧头
     */
    public final static byte HEAD = (byte) 0xF8;
    /**
     * 帧尾
     */
    public final static byte TAIL = 0x0D;

    /**
     * 构造函数
     */
    public DataFrame() {
        buf = null;
        length = 0;
    }

    /**
     * 构建数据帧
     *
     * @param buf
     */
    public DataFrame(byte[] buf, short length) {
        this.buf = buf;
        this.length = length;
    }

    /**
     * 获取帧首地址
     *
     * @return
     */
    public byte[] getBuf() {
        byte[] result = new byte[length];
        System.arraycopy(buf,0, result,0,length);
        return result;
    }

    /**
     * 获取帧长度
     *
     * @return
     */
    public short getLength() {
        return length;
    }

    /**
     * 重建校验码
     */
    public void rebuildCs() {
        if (this.length < 2) return;

        buf[this.length - 2] = ProtocolUtils.CheckXor(buf,1, length - 3);;
    }
}
